package astar;

import domain.Agent;
import domain.Board;
import domain.Flag;
import domain.Game.direction;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Vector;

/**
 *
 * @author shaigi
 */
public class AStar {

    private Agent _agent;
    private PriorityQueue<ANode>    _priorityQ;
    private int _counter;
    
    public AStar(Agent agent) {
        this._agent = agent;
        this._priorityQ = new PriorityQueue<ANode>();
        this._counter = 41;
    }
    
    public LinkedList<direction> runAStarSearch(Board board, Vector<Agent> agents, Vector<Flag> flags){
        LinkedList<direction> ans = new LinkedList<direction>();
        
        //constructing first node of A* tree:
        ANode top = new ANode(agents, flags, 0, ans);
        _priorityQ.add(top);
        
        
        //TODO
        
        
        while(!endCondition()){
            ANode a = _priorityQ.poll();//extract with minimal f function
            
            
            //Extract
            ANode a_up      = a.simulateNode(direction.UP, _agent);
            ANode a_right   = a.simulateNode(direction.RIGHT, _agent);
            ANode a_down    = a.simulateNode(direction.DOWN, _agent);
            ANode a_left    = a.simulateNode(direction.LEFT, _agent);
            
            if(a_up!=null){
                _priorityQ.add(a_up);
            }
            if(a_right!=null){
               _priorityQ.add(a_right);
            }
            if(a_down!=null){
                _priorityQ.add(a_down);
            }
            if(a_left!=null){
                _priorityQ.add(a_left);
            }
            //end of extract
            
            
        }
        
        ans = _priorityQ.peek().getPath();
        return ans;
    }

    private boolean endCondition() {
        this._counter--;
        
        return this._counter == 0;
        //TODO: check if there is more flag
        //TODO: check if you are dead
        
    }
    
    
}
